import 'dart:io';

import 'package:alipay_kit/alipay_kit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_nb_net/flutter_net.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:wechat_kit/wechat_kit.dart';
import 'package:tracker/pages/home/vip_service/view/vip_service_page.dart';

import '../../../../common/constants/app_constants.dart';
import '../../../../common/log/x_logger.dart';
import '../../../../common/pay/ali_pay_utils.dart';
import '../../../../common/pay/model/alipay_order_info.dart';
import '../../../../common/pay/wxpay_utils.dart';
import '../../../../provider/global_provider.dart';
import '../../../../utils/toast_utils.dart';

mixin VipServicePageMixin<T extends ConsumerStatefulWidget>
    on ConsumerState<T> {
  List<String> tabs = ["安防录音", "高清录音", "录音恢复"];
  final AlipayKitPlatform _alipay = AlipayKitPlatform.instance;

  late TabController tabController;

  final alipayUtils = AliPayUtils();
  final wxpayUtils = WxpayUtils();

  @override
  void initState() {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      wxpayUtils.registerWechatResp((resp) {
        XLogger.info('wxpayUtils exMsg: ${resp}');
        XLogger.info('wxpayUtils exMsg: ${resp.errorMsg}');

          // XLogger.info('拉起小程序: ${resp.errorCode} ${resp.errorMsg}, isSuccessful: ${resp.isSuccessful}');
          // XLogger.info('拉起小程序 exMsg: ${resp.extMsg}');
          if (resp.isSuccessful) {
            // 支付成功
            ToastUtils.showSimpleDialog(context,
                title: '提示', content: '套餐购买成功', confirmTitle: '好的', confirmCallback: () {});
            // 刷新套餐信息
            _fetchDeviceInfo();
            XLogger.info('购买成功');
          } else {
            // 支付失败
            ToastUtils.showToast('支付失败');
          }
      });
    });
    alipayUtils.listenPay((resp) {
      XLogger.info('VIP支付结果：${resp.resultStatus}');
      if (resp.isSuccessful) {
        // 支付成功
        // ToastUtils.showSimpleDialog(context,
        //     title: '提示', content: '套餐购买成功', confirmTitle: '好的', confirmCallback: () {});
        ToastUtils.showToast('购买成功');
        // 刷新套餐信息
        _fetchDeviceInfo();
      } else {
        // 支付失败
        ToastUtils.showToast('支付失败');
      }
    });
    super.initState();
  }

  Future<void> _fetchDeviceInfo() async {
    await ref.read(deviceInfoProvider.notifier).refresh(() {});


    // final PaymentSuccessCallback? onPaymentSuccess;

  }
  void createVip(int? packageId, String imei) async {
    // 判断是否安装支付宝
    final bool isInstalled = await _alipay.isInstalled();
    if (!isInstalled) {
      ToastUtils.showToast('请先安装支付宝');
      return;
    }
    final res = await get<AlipayOrderInfoResp, AlipayOrderInfoResp>(
        "$newBaseULr/ali/appPay?imei=${imei.toString()}&packageId=$packageId",
        decodeType: AlipayOrderInfoResp());
    res.when(success: (resp) {
      ToastUtils.dismissLoading();
      if (resp.code == 200) {
        // 创建订单成功
        if (Platform.isAndroid) {
          _alipay.setEnv(env: AlipayEnv.online);
        }
        _alipay.pay(orderInfo: resp.data ?? "",dynamicLaunch: true);
      } else {
        ToastUtils.showToast(resp.msg ?? '创建订单失败');
      }
    }, failure: (String msg, int code) {
      ToastUtils.showToast('创建订单失败');
    });
  }

  @override
  void dispose() {
    super.dispose();
    alipayUtils.dispose();
    wxpayUtils.dispose();
  }
}

